package com.heroh.kettle.util;

import com.heroh.kettle.entity.ParamDbConnection;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.trans.TransMeta;

import java.util.List;
import java.util.Map;

public class MetaBuild {

    private TransMeta transMeta;

    public MetaBuild() {
        try {
            KettleEnvironment.init();
            this.transMeta = new TransMeta();
        } catch (KettleException e) {
            e.printStackTrace();
            throw new RuntimeException("kettle 配置异常");
        }
    }

    public MetaBuild(String fileName) {
        try {
            KettleEnvironment.init();
            this.transMeta = new TransMeta(fileName);
        } catch (KettleException e) {
            e.printStackTrace();
            throw new RuntimeException("kettle 配置异常");
        }
    }

    public MetaBuild setConns(List<ParamDbConnection> conns) {
        KettleCommonUtil.setDatabases(transMeta, conns);
        return this;
    }

    public MetaBuild setConns(List<ParamDbConnection> conns, Map<String, String> dbMap) {
        KettleCommonUtil.updateDatabases(transMeta, conns, dbMap);
        return this;
    }

    public MetaBuild setTransLog() {
        // 设置转换日志
        KettleTransLog.setTransLog(transMeta);
        return this;
    }

    public TransMeta build() {
        return transMeta;
    }
}
